﻿/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: FZH
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Domain.IRepository.MainData;
using FriendStar.Repository.MainData;

namespace FriendStar.Application.MainData
{
    /// <summary>
	/// 数据表 CLK_81_CB_ReserveDevice 对应的数据应用功能类。
	/// </summary>
	public class CLK_81_CB_ReserveDeviceApp
    {       
        private string UseUnit = Configs.GetValue("UseUnit");  

        private ICLK_81_CB_ReserveDeviceRepository service = new CLK_81_CB_ReserveDeviceRepository();

        public CLK_81_CB_ReserveDeviceEntity GetForm(string MaterialCode, string MaterialSizes, string FProCompanyNO, string StandardNumber, string MaterialDemand, string MaterialNO)
        {
            var expression = ExtLinq.True<CLK_81_CB_ReserveDeviceEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.MaterialCode == MaterialCode);
            expression = expression.And(t => t.MaterialSizes == MaterialSizes);
            expression = expression.And(t => t.FProCompanyNO == FProCompanyNO);
            expression = expression.And(t => t.StandardNumber == StandardNumber);
            expression = expression.And(t => t.MaterialDemand == MaterialDemand);
            expression = expression.And(t => t.MaterialNO == MaterialNO);
            return service.FindEntity(expression);
        }

        public void DeleteForm(string MaterialCode, string MaterialSizes, string FProCompanyNO, string StandardNumber, string MaterialDemand, string MaterialNO)
        {
            var expression = ExtLinq.True<CLK_81_CB_ReserveDeviceEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.MaterialCode == MaterialCode);
            expression = expression.And(t => t.MaterialSizes == MaterialSizes);
            expression = expression.And(t => t.FProCompanyNO == FProCompanyNO);
            expression = expression.And(t => t.StandardNumber == StandardNumber);
            expression = expression.And(t => t.MaterialDemand == MaterialDemand);
            expression = expression.And(t => t.MaterialNO == MaterialNO);
            service.Delete(expression);
        }

        public void SubmitForm(CLK_81_CB_ReserveDeviceEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.ReserveID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public void FullUpdate(CLK_81_CB_ReserveDeviceEntity entity)
        {
            var expression = ExtLinq.True<CLK_81_CB_ReserveDeviceEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.MaterialCode == entity.MaterialCode);
            expression = expression.And(t => t.MaterialSizes == entity.MaterialSizes);
            expression = expression.And(t => t.FProCompanyNO == entity.FProCompanyNO);
            expression = expression.And(t => t.StandardNumber == entity.StandardNumber);
            expression = expression.And(t => t.MaterialDemand == entity.MaterialDemand);
            expression = expression.And(t => t.MaterialNO == entity.MaterialNO);
            var obj = new { FLowestStore = entity.FLowestStore, FHighestStore = entity.FHighestStore, FSafetystore = entity.FSafetystore, FCalcuStore = entity.FCalcuStore, FStoreDates = entity.FStoreDates };
            service.Update(obj, expression);
        }

        public int SetStoProReserviorStatus(string keyValue)
        {
            string sql = "UPDATE CLK_81_CB_ReserveDevice SET FMaintainStatus='1',FAuditingBy='" + OperatorProvider.Provider.GetCurrent().UserName + "',FAuditingDate='" + DateTime.Now.ToString("yyyy-MM-dd") + "',FAuditingTime='" + DateTime.Now.ToString("HH:mm:ss") + "' WHERE ReserveID in (" + keyValue + ")";
            return service.ExecSql(sql);
        }
    }
}